﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.HtmlControls;

namespace Vit.Report.XmlWebReport
{
    public class TableReportRenderer :IReportRender
    {
        private HtmlTable table;

        #region IReportRender Members

        public void RenderCell(TextWriter writer, Page page, RectanglePosition pos, RectangleF rect, string id, string innerContent, string cssClass, string style, int zIndex, Font font)
        {
            if (table == null)
            {
                table = new HtmlTable();
                table.ID = string.Format("{0}_table", page.ID);
                table.Border = 0;
                table.CellPadding = 0;
                table.CellSpacing = 0;
                table.Style.Add("margin",
                    string.Format("{1}{0} {2}{0} {3}{0} {4}{0}", page.Unit,
                    page.PageMargin.Top, // - page.CellSize.Height, 
                    page.PageMargin.Right, 
                    page.PageMargin.Bottom, 
                    page.PageMargin.Left));
                table.Style.Add("position", "absolute");
                table.Style.Add("z-index", "99");
                table.Style.Add("border-collapse", "collapse");
                //table.Style.Add("width", string.Format("{0}{1}", page.ContentSize.Width, page.Unit));
                //table.Style.Add("height", string.Format("{0}{1}", page.ContentSize.Height, page.Unit));

                //HtmlTableRow headerRow = new HtmlTableRow();
                //for (int i = 0; i < page.RectanglePosition.XSpan; i++)
                //{
                //    HtmlTableCell headerCell = new HtmlTableCell();
                //    headerCell.Style.Add("width", string.Format("{0}mm", page.CellSize.Width));
                //    headerCell.Style.Add("height", string.Format("{0}mm", page.CellSize.Height));
                //    headerCell.InnerText = " ";
                //    headerRow.Cells.Add(headerCell);
                //}
                //table.Rows.Add(headerRow);
            }

            HtmlTableRow row = null;
            foreach (HtmlTableRow r in table.Rows)
            {
                if (r.ID == string.Format("row_{0}", pos.TopIndex))
                {
                    row = r;
                    break;
                }
            }

            if (row == null)
            {
                row = new HtmlTableRow();
                row.ID = string.Format("row_{0}", pos.TopIndex);
                table.Rows.Add(row);
            }

            HtmlTableCell cell = new HtmlTableCell();
            cell.ID = string.Format("row_{0}_cell_{1}", pos.TopIndex, pos.LeftIndex - 'A' + 1);
            cell.Attributes.Add("pos", string.Format("{0}_{1}", pos.TopLeftPosition, pos.BottomRightPosition));
            cell.RowSpan = pos.YSpan;
            cell.ColSpan = pos.XSpan;
            cell.Attributes.Add("class", cssClass);
            cell.Attributes.Add("style", style);
            cell.Style.Add("font", ReportHelper.ToHtmlFont(font));
            //cell.Style.Add("width", string.Format("{0}mm", page.CellSize.Width * pos.XSpan));
            cell.Style.Add("height", string.Format("{0}mm", page.CellSize.Height * pos.YSpan));

            cell.InnerHtml = innerContent;

            row.Cells.Add(cell);
        }

        public void RenderPage(TextWriter writer, Page page)
        {
            foreach (HtmlTableRow row in table.Rows)
            {
                if (row.Cells.Count <= 0)
                    continue;
                bool sameRowSpan = true;
                int rowSapn = row.Cells[0].RowSpan;
                foreach (HtmlTableCell cell in row.Cells)
                {
                    if (cell.RowSpan != rowSapn)
                    {
                        sameRowSpan = false;
                        break;
                    }
                }

                if (sameRowSpan)
                {
                    foreach (HtmlTableCell cell in row.Cells)
                    {
                        cell.RowSpan = 1;
                    }
                }
            }
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            table.RenderControl(htmlWriter);
        }

        #endregion


    }
}
